<?php
$contactdata = $teammember->contactdata();
$teams = $teammember->team();
$team = $teams[0];
$department = $team->department();
$cost_center = $teammember->costcenter();
$actual_jobcluster = Model\Jobcluster::find($teammember->actual_jobcluster_id, FALSE);
$target_jobcluster = Model\Jobcluster::find($teammember->target_jobcluster_id, FALSE);
$actual_expertgroup = Model\Expertgroup::find($teammember->actual_expertgroup_id, FALSE);
$target_expertgroup = Model\Expertgroup::find($teammember->target_expertgroup_id, FALSE);
$demand = $teammember->stopedemand();
$notes = $teammember->note();

?>

<table class="table table-condensed labels-centered">

    <tr>
        <td>Anrede</td>
        <td><?php if ($contactdata->title === 0) echo 'Herr'; else if ($contactdata->title === 1) echo 'Frau'; else echo '-'; ?></td>
    </tr>
    <tr>
        <td>Personalnummer</td>
        <td><?php echo $teammember->id; ?></td>
    </tr>
    <tr>
        <td>Vorname</td>
        <td><?php echo $contactdata->forename; ?></td>
    </tr>
    <tr>
        <td>Nachname</td>
        <td><?php echo $contactdata->surname; ?></td>
    </tr>
    <tr>
        <td>E-Mail Adresse</td>
        <td><a href="mailto:<?php echo $contactdata->email; ?>"><?php echo $contactdata->email; ?></a></td>
    </tr>
    <tr>
        <td>Abteilung</td>
        <td><?php echo $department->name; ?></td>
    </tr>
    <tr>
        <td>Team</td>
        <td><?php echo $team->name; ?></td>
    </tr>
    <tr>
        <td>Kostenstelle</td>
        <td><?php echo $cost_center->id; ?></td>
    </tr>
    <!--<tr><td>Rolle/Rechte</td><td>Administrator</td></tr>
    <tr><td>Registriert seit</td><td>12.03.2012</td></tr>-->
</table>

<?php if(isset($mode) && $mode === 'edit_istplan'): ?>

    <?php echo form_open('/team/edit_istplan/'); ?>

    <?php echo form_hidden('teammember_id', $teammember->id); ?>
    <table class="table table-condensed">
        <tr><th></th><th>IST</th><th>PLAN</th></tr>
        <tr>
            <td>Jobcluster</td>
            <td><?php echo form_dropdown('act_jc', $all_jobclusters, $actual_jobcluster->id); ?></td>
            <td><?php echo form_dropdown('tar_jc', $all_jobclusters, $target_jobcluster->id); ?></td>
        </tr>
        <tr>
            <td>Expertgroup</td>
            <td><?php echo form_dropdown('act_eg', $all_expertgroups, $actual_expertgroup->id); ?></td>
            <td><?php echo form_dropdown('tar_eg', $all_expertgroups, $target_expertgroup->id); ?></td>
        </tr>
    </table>
    <?php echo form_submit('', 'Übernehmen', 'class="btn btn-success btn-small" style="float:right"'); ?>
    <?php echo form_close('<br style="clear:right">'); ?>

<?php else: ?>

<table class="table table-condensed">
    <tr><th></th><th>IST</th><th>PLAN</th></tr>
    <tr>
        <td>Jobcluster</td>
        <td><?php echo $actual_jobcluster ? $actual_jobcluster->name : 'N/A'; ?></td>
        <td><?php echo $target_jobcluster ? $target_jobcluster->name : 'N/A'; ?></td>
    </tr>
    <tr>
        <td>Expertgroup</td>
        <td><?php echo $actual_expertgroup ? $actual_expertgroup->name : 'N/A'; ?></td>
        <td><?php echo $target_expertgroup ? $target_expertgroup->name : 'N/A'; ?></td>
    </tr>
</table>
<?php echo anchor('team/show_member/'.$teammember->id.'?mode=edit_istplan', 'IST-PLAN ändern', 'class="btn btn-small"'); ?>
<?php endif; ?>


<fieldset>
    <legend>Bemerkungen</legend>
    <?php if (isset($notes) AND !empty($notes)): ?>
    <table class="table table-condensed table-bordered" style="font-size:14px; margin-bottom: 3px">
        <tr><th style="width:140px">Hinzugefügt am</th><th>Notiz</th></tr>
        <?php foreach ($notes as $note): ?>
                <tr>
                    <td><?php echo date("d.m.Y H:i:s", strtotime($note->metadata()->created)); ?></td>
                    <td><?php echo $note->text; ?></td>
                </tr>
        <?php endforeach; ?>
    </table>
    <?php else: ?>
        <p>Keine Kommentare zu diesem Teammitglied...</p>
    <?php endif; 
    
    echo form_button('', 'Notiz hinzufügen <img src="./../../../img/carret_down.png">', 'class="btn btn-mini" id="btn_add_note" style="margin-bottom:3px"'); 
    
    echo form_open($form['action'], 'id="form_add_note"');
    echo form_textarea($form['comment']);
    echo '<br>';
    echo form_submit($form['add_comment']);
    echo form_close();
    ?>
</fieldset>


<fieldset>
    <legend>Schulungen</legend>
    <br >

<?php if (isset($sm_status) AND $sm_status == 'success') echo '<div class="alert alert-success" style="width:600px">Schulung erfolgreich getauscht!</div>'; ?>

    <table class="table table condensed td-vcenter" >
    <?php $counter = 1 ?>
    <?php foreach ($demand as $item): ?>
    <?php if (!$item->inactive) $off = TRUE; else $off = FALSE; ?>
            <tr>
                <td <?php if ($off) echo 'class="blocked"'; ?>>#<?php echo $counter; ?></td>
                <td><!--<div class="dot dot-green"></div>--></td>
                <td  <?php if ($off) echo 'class="blocked"'; ?>>
    <?php
	if ($item->course_id && $item->course() && $item->course()->traininggroup()) {
		
		$traininggroups_arr = $item->course()->traininggroup();
		$traininggroupid_arr = array();
		foreach($traininggroups_arr as $traininggroup)
		{
			$traininggroupid_arr[] = $traininggroup->id;
		}
		
		foreach($teammember->training() as $training) {
			if($training->canceled != 1 && in_array($training->traininggroup()->id, $traininggroupid_arr)) {
				echo anchor('training/details/'.$training->id, $item->course()->title);
				break;
			}
			else {
				continue;
			}
		}
	}
    else if ($item->course_id !== NULL) {
        echo anchor('catalogue/details/' . $item->course_id, $item->course()->title);
	}
    else {
        echo $item->title;
	}
    ?>
                    <?php if ($item->tag()->name == 'STR'): ?>
                        <span class="badge-inhouse label label-success">STR</span>
                    <?php endif; ?>
                </td>
                <td>
                </td>
                <td  <?php if ($off) echo 'class="blocked"'; ?>>
                    <?php if ($item->course_id !== NULL): ?>
                        Einzelpreis: <?php
                if ($item->course() && $item->course()->traininggroup()) {
                    echo number_format($item->course()->cost()->amount, 2, ',', '.') . '€';
                } else if ($item->course()) {
                    echo number_format($item->course()->cost()->amount, 2, ',', '.') . '€';
                }
                        ?>
                    <?php endif; ?>
                </td>
                <td>
                    <?php
                    if ($off)
                        echo anchor('/team/activate_stope/' . $item->id . '?target=' . $target, '<button class="btn" type="button">Aktivieren</button>');
                    else
                        echo anchor('/team/deactivate_stope/' . $item->id . '?target=' . $target, '<button class="btn" type="button">Deaktivieren</button>');
                    ?>
    <?php echo anchor('catalogue/show?action=switch&stopeid=' . $item->id, '<button class="btn" type="button">Schulung austauschen</button>'); ?> 
                </td>
            </tr>

                    <?php $counter++; ?>
                <?php endforeach; ?>
    </table>

</fieldset>

<fieldset>
    <legend>Aktivität</legend>
    <button id="show_history" class="btn btn-mini btn-info" type="button">Aktivität einblenden</button>
    <div id="history_table">
        <table class="table-mini labels-magenta">
            <tr><td>13.12.2012 - 15:23:12</td><td>Mitarbeiter wurde durch Import an Schulung(ID:23) geknüpft.</td></tr>
            <tr><td>14.12.2012 - 15:30:12</td><td>Mitarbeiter hat zu Team "BC+P" gewechselt.</td></tr>
            <tr><td>15.12.2012 - 09:00:01</td><td>Schulung (ID:45) wurde durchgeführt.</td></tr>
            <tr><td>16.12.2012 - 10:15:02</td><td>Teamleiter setzte Schulung(ID: 23) auf 'inaktiv'</td></tr>
            <tr><td>17.12.2012 - 13:34:56</td><td>Geplante Schulung "SOA" wurde durch Teamleiter mit "SCRUM" ausgetauscht</td></tr>
        </table>
    </div>


</fieldset>